package com.c.ctgproject.ui.storage

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.mode.StorageVehicles
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.ui.other.InputPlateActivity
import com.c.ctgproject.ui.storage.car.StorageListActivity
import com.c.ctgproject.ui.storage.car.VehicleInspectionListActivity
import com.c.ctgproject.ui.storage.material.StorageMaterialInActivity
import com.c.ctgproject.ui.storage.material.StorageMaterialOutActivity
import com.c.ctgproject.ui.storage.part.StoragePartsOutActivity
import com.c.ctgproject.ui.vehicleinspection.VehicleInspectiontActivity
import com.c.ctgproject.ui.vehiclelocation.VehicleLocationActivity
import com.c.ctgproject.utils.*
import com.c.ctgproject.utils.pickerimage.utils.RequestCode
import com.yzq.zxinglibrary.common.Constant
import kotlinx.android.synthetic.main.activity_storage_home.*
import org.jetbrains.anko.startActivity
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2020/9/14
 *描述:仓储 首页
 */
class StorageHomeActivity : BaseActivity() {
    private val mViewModel: StorageHomeViewModel by lazy { ViewModelProvider(this)[StorageHomeViewModel::class.java] }
    private var carPlate = ""
    override fun getLayout() = R.layout.activity_storage_home
    override fun event() {
        changeTransparentStatusBar()
        //车牌拍摄OCR识别回调
        mViewModel.responseData.observe(this, Observer { response ->
            loading.dismiss()
            if (response.status == 0) {
                mViewModel.primaryInspection(userToken, response.data.toString())
                carPlate = response.data.toString()
                Log.e("data", response.data.toString())
            } else {
                CustomToast.showToast(this, response.msg)
                //OCR识别失败了 进入手动输入车牌
                startActivity<InputPlateActivity>()
            }
        })

        mViewModel.storageVehiclesData.observe(this, Observer { response: Response<StorageVehicles> ->
            if (response.status == 1) {
                showToast(response.msg)
            } else {
                val intent = Intent(applicationContext, VehicleInspectiontActivity::class.java)
                intent.putExtra("carPlate", response.data.ownerPlate)
                startActivity(intent)
            }
        })

        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            showToast(it)
        })

    }

    override fun click() {
        //车辆初检
        mCarFirstCheckOutLL.setOnClickListener {
            //拍照识别车牌
//            val intent = Intent(this, CameraActivity::class.java)
//            startActivityForResult(intent, RequestCode.TAKE_PHOTO)
            //原扫描车牌改为列表展示

            startActivity<VehicleInspectionListActivity>()
        }

        //物料入库
        mMaterialInLL.setOnClickListener {
            startActivity<StorageMaterialInActivity>()
        }
        //配件出库
        mPartOutLL.setOnClickListener {
            startActivity<StoragePartsOutActivity>()
        }
        //物料出库
        mMaterialOutLL.setOnClickListener {
            startActivity<StorageMaterialOutActivity>()
        }
        //设置和角色切换
        mSettingIV.setOnClickListener {
            showPopupWindow(it)
        }
        //车辆扫码
        mScanQrLL.setOnClickListener {
            scanQr()
        }
        //车辆列表
        mCarListLL.setOnClickListener {
            startActivity<StorageListActivity>()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (resultCode == Activity.RESULT_OK && requestCode == QR_CODE_RESULT && data != null) {
            val content: String? = data.getStringExtra(Constant.CODED_CONTENT)
            MyLog.d("二维码扫描出的信息:$content")
            content?.let {
                val strList: List<String> = content.split(":")
                if (strList.size <= 1) {
                    showToast("二维码不包含库位信息!")
                } else {
                    if (strList[0].replace("\uFEFF", "") == "carCode") {
                        startActivity<VehicleLocationActivity>("carCode" to strList[1])
                    } else {
                        showToast("请扫拓号单二维码！")
                    }
                }
            }
        }

        if (requestCode == RequestCode.TAKE_PHOTO && data != null) {
            MyLog.d("进入回调")
            val filePath = data.getStringExtra("take_photo")
            filePath?.let {
                val file = File(filePath)
//                val requestFile: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
//                val part = MultipartBody.Part.createFormData("file", file.name, requestFile)
                loading.show()
                Log.d("日志", "开始上传")
                //上传照片
                mViewModel.licensePlate(userToken, file)
            }
        }
    }

    override fun onBackPressed() {
        exitApp()
    }
}